﻿using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace WebCrawler.Console
{
    class Program
    {
        private static IUnityContainer _rootContainer = new UnityContainer();

        public static void Main(string[] args)
        {
            var commandLineProcessor = new CommandLineProcessor();
            if (!commandLineProcessor.TryParse(args))
            {
                var msg = commandLineProcessor.GetHelpMessage();
                System.Console.WriteLine(msg);
                return;
            }

            _rootContainer.LoadConfiguration("root");
            var crawler = _rootContainer.Resolve<IWebCrawler>();
            var logger = _rootContainer.Resolve<IUrlLogger>();
            logger.Start();

            var pages = crawler.StartFrom(commandLineProcessor.StartUrl);
            foreach (var page in pages)
            {
                if (page.IsSuccess())
                {
                    System.Console.WriteLine(page.Url);
                    logger.WriteLine(page.Url);
                }
            }

            logger.Stop();

            System.Console.WriteLine("Press <enter> to quit.");
            System.Console.ReadLine();
        }
    }
}
